package cn.edu.cuit.elena.web;

import java.util.HashMap;
import java.util.Map;

import cn.edu.cuit.elena.common.Configuration;
import cn.edu.cuit.elena.common.SackConstants;
import cn.edu.cuit.elena.db.DataSource;
import cn.edu.cuit.elena.db.DataSourceFactory;
import cn.edu.cuit.elena.db.dao.BaseDao;
import cn.edu.cuit.elena.transaction.controller.TaskMonitor;
import cn.edu.cuit.elena.transaction.query.HiveQueryExecutor;

/**
 * Application level system resources provider.
 * */
public class System
{
    private static DataSource systemDataSource;
    private static Configuration configuration;
    private static BaseDao baseDao;
    private static TaskMonitor taskMonitor;
    private static HiveQueryExecutor hiveQueryExecutor;
    private static BaseDao hiveRemote;

    static
    {
        synchronized( System.class )
        {
            configuration = Configuration.instance();
            systemDataSource = DataSourceFactory.createDataSourceByConfiguration( configuration );
            baseDao = new BaseDao( systemDataSource );
            taskMonitor = TaskMonitor.initilize();
            taskMonitor.start();
            hiveQueryExecutor = HiveQueryExecutor.getInstance( taskMonitor );
            Map<String, String> map = new HashMap<String, String>();
            map.put( SackConstants.URL, "jdbc:mysql://localhost:3306/hive_remote" );
            map.put( SackConstants.USER_NAME, "root" );
            map.put( SackConstants.PASS_WORD, "z44569565" );
            map.put( SackConstants.DRIVER, "com.mysql.jdbc.Driver" );
            hiveRemote = new BaseDao( DataSourceFactory.createDataSourceByConfiguration( Configuration.create( map ) ) );
        }
    }

    public static TaskMonitor getSystemTaskMonitor()
    {
        return taskMonitor;
    }

    public static DataSource getSystemDataSource()
    {
        return systemDataSource;
    }

    public static Configuration getConfiguration()
    {
        return configuration;
    }

    public static BaseDao getBaseDao()
    {
        return baseDao;
    }

    public static HiveQueryExecutor getHiveQueryExecutor()
    {
        return hiveQueryExecutor;
    }

    public static BaseDao getHiveRemote()
    {
        return hiveRemote;
    }
}
